package com.ktg.mes.print.Controller;


import com.ktg.common.annotation.Log;
import com.ktg.common.core.controller.BaseController;
import com.ktg.common.core.domain.APIResult;
import com.ktg.common.core.domain.Result;
import com.ktg.mes.md.domain.MdProductBom;
import com.ktg.mes.print.service.impl.IXZPrintServiceImpl;
import com.ktg.mes.pro.domain.ProWorkorder;
import com.ktg.mes.pro.service.IProWorkorderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.List;

@RestController
@RequestMapping("/mes/print/xz")
public class XZPrintController extends BaseController {


    @Autowired
    private IXZPrintServiceImpl ixzPrintService;

    @Autowired
    private IProWorkorderService iProWorkorderService;



    /**
     * 获取 生产排产跟进单
     */
    @PreAuthorize("@ss.hasPermi('mes:print:xz:scheduling')")
    @Log(title = "生产排产跟进单")
    @GetMapping("/scheduling")
    public Result scheduling(@RequestParam(name="workorderId", required = true) Long workorderId, HttpServletRequest req)
    {
        APIResult apiLog = new APIResult("生产排产跟进单",req,workorderId);
        try {
            List<MdProductBom> bomList = ixzPrintService.selectBomByWorkorderId(String.valueOf(workorderId));

            //获取当前排产订单
            ProWorkorder proWorkorder = iProWorkorderService.selectProWorkorderByWorkorderId(workorderId);

            //统计用量
            for (int i = 0;i < bomList.size();i++){
                MdProductBom bom = bomList.get(i);
                if (bom.getQuantity() != null && proWorkorder.getQuantity() != null){
                    float aa = bom.getQuantity().floatValue()*proWorkorder.getQuantity().floatValue();
                    bom.setQuantity(BigDecimal.valueOf(aa));
                }
            }
            return apiLog.ok(bomList);
        }catch (Exception e){
            return apiLog.error(e);
        }
    }


}
